package com.donica.wamstest.Controller;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.donica.wamstest.entity.CommonFindVO;
import com.donica.wamstest.entity.ReturnMessage;
import com.donica.wamstest.service.CloudFileService;
import com.donica.wamstest.utils.FileHandleUtil;

import io.swagger.annotations.Api;

/*
 * @author laifuying
 * @version 1.0
 * @date  2020/2/8 22:42
 * @description 云端文件控制器
 */
@RestController
@RequestMapping("/api/v1/")
@Api(value = "CloudFileController", tags = "云端文件控制器")
public class CloudFileController {
	@Autowired
	private CloudFileService cloudFileService;

	// 扫描服务器文件
	@RequestMapping(value = "programs/scan", method = RequestMethod.GET)
	public ReturnMessage scanProrams() {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {
			cloudFileService.ScanProgram();
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		return rtnMsg;
	}

	// 扫描服务器文件
	@RequestMapping(value = "softwares/scan", method = RequestMethod.GET)
	public ReturnMessage scanSoftwares() {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {
			cloudFileService.ScanSoftware();
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		return rtnMsg;
	}

	// 查询云上节目包列表
	@RequestMapping(value = "programs/list/{pageSize}/{currPage}", method = RequestMethod.POST)
	public ReturnMessage searchProrams(@PathVariable("pageSize") int pageSize, @PathVariable("currPage") int currPage,
			@RequestBody CommonFindVO vo) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {

			int firstIndex = pageSize * currPage;
			vo.setFirstIndex(firstIndex);
			vo.setPageSize(pageSize);
			vo.setCurrPage(currPage);

			rtnMsg = cloudFileService.searchProgramPackage(vo);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		return rtnMsg;
	}

	// 查询云上软件包列表
	@RequestMapping(value = "softwares/list/{pageSize}/{currPage}", method = RequestMethod.POST)
	public ReturnMessage searchSoftwares(@PathVariable("pageSize") int pageSize, @PathVariable("currPage") int currPage,
			@RequestBody CommonFindVO vo) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {

			int firstIndex = pageSize * currPage;
			vo.setFirstIndex(firstIndex);
			vo.setPageSize(pageSize);
			vo.setCurrPage(currPage);

			rtnMsg = cloudFileService.searchSoftwarePackage(vo);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		return rtnMsg;
	}

	/**
	 * 测试文件上传
	 */
	@RequestMapping(value = "logs/upload", method = RequestMethod.POST)
	@ResponseBody
	public String uploadlogs(@RequestParam("file") MultipartFile file) throws IOException {
		System.out.println("OriginalFilename:" + file.getOriginalFilename());
		String fileURL = FileHandleUtil.upload(file.getInputStream(), "wamslogs/", file.getOriginalFilename());
		return "文件存放路径为: " + fileURL;
	}
}
